package com.codeye.spider.accessFlag;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.codeye.audio.AudioFilePlayer;
import com.codeye.spider.Queue;

public class Actions_aa {

	private static boolean accessAble(String htmlStr) {
		if (null != htmlStr) {
			if (htmlStr.indexOf("Local function for getting a cookie value") != -1) {
				return false;
			} else if (htmlStr.indexOf("114导航") != -1) {
				return false;
			} else if (htmlStr.indexOf("not found") != -1) {
				return false;
			} else if (htmlStr.indexOf("404 Not Found") != -1) {
				return false;
			} else if (htmlStr.indexOf("403 Forbidden") != -1) {
				return false;
			} else if (htmlStr.indexOf("500 Internal Server Error") != -1) {
				return false;
			} else if (htmlStr.indexOf("400 Bad Request") != -1) {
				return false;
			} else if (htmlStr.indexOf("parking") != -1) {
				return false;
			} else if (htmlStr.indexOf("alww") != -1) {
				return false;
			} else if (htmlStr.indexOf("alreject") != -1) {
				return false;
			} else if (htmlStr.indexOf("您打开的链接不存在") != -1) {
				return false;
			}
		} else {
			return false;
		}
		return true;
	}

	public static String grabWebName(String Url, int index, int restSize) {
		String fullUlr = "http://www." + Url;
		String htmlStr = null;
		try {
			htmlStr = getHtmlContent(fullUlr, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String result = "ok";

		if (!accessAble(htmlStr)) {
			fullUlr = "http://" + Url;
			try {
				htmlStr = getHtmlContent(fullUlr, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (!accessAble(htmlStr)) {
				result = "不可访问";
			}
		}
		// ItemQueue.addItem(Url, result);
		if (result.equals("ok")) {
			System.out.println(">>>>>>>>>>>>>>>>>> 【"+restSize+"】【"+index+"号】："+Url+" >>> "+result);
		} else {
			System.err.println(">>>>>>>>>>>>>>>>>> 【"+restSize+"】【"+index+"号】："+Url+" >>> "+result);
		}
		return result;
	}

	private static String getHtmlContent(String url, String charset) throws UnsupportedEncodingException {
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(5000)
				.setConnectTimeout(5000)
				.setConnectionRequestTimeout(5000).build();
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("account", ""));
		formparams.add(new BasicNameValuePair("password", ""));
//		HttpEntity reqEntity = new UrlEncodedFormEntity(formparams, "utf-8");

//		HttpPost hpost = new HttpPost(url);
		HttpGet hget = new HttpGet(url);
		hget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0");  
//		post.setEntity(reqEntity);
		hget.setConfig(requestConfig);

		// CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();

		String htmlContent = null;
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(hget);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode == HttpStatus.SC_OK) {
				HttpEntity resEntity = response.getEntity();
				htmlContent = EntityUtils.toString(resEntity, "utf-8");
			} else {
				System.err.println("Method failed: " + response.getStatusLine() + " " + url);
			}
			
		} catch (ConnectException e) {
			System.err.println("ConnectException >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + url);
		} catch (IOException e) {
			System.err.println("IOException >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + url);
		} catch (IllegalArgumentException e) {
			System.err.println("IllegalArgumentException >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + url);
		} finally {
			try {
				if(null != response){
					response.close();
				}
				httpClient.close();
			} catch (IOException e) {
				System.err.println("IOException!        close");
			}
		}
		return htmlContent;
	}

	public static void main(String[] args) throws SQLException {
		List<String> urlList = new ArrayList<String>();
		urlList.add("lywhxy.com");
		urlList.add("lzlqc.com");
		urlList.add("gdxzzy.cn");
		urlList.add("kmyz.edu.cn");
		urlList.add("bhcy.cn");
		urlList.add("hnswxy.com");
		urlList.add("shxj.cn");
		
		AudioFilePlayer ap = new AudioFilePlayer();
		ap.play("F:/KZ_Music/音效/坦克驾驶员.wav");
		for (int i = 0; i < urlList.size(); i++) {
			String url = urlList.get(i);
			grabWebName(url, i, Queue.size());
		}
		ap.play("F:/KZ_Music/音效/Reserch complete.wav");
		ap.play("F:/KZ_Music/音效/Upgrade complete.wav");
		ap.play("F:/KZ_Music/音效/job finish.wav");
	}
}